using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using CMS_Admin.Backend.Api.Entity;
using CMS_Admin.Backend.Api.ParamModel;
using Microsoft.IdentityModel.Tokens;

namespace CMS_Admin.Backend.Api.Utils
{
    public class TokenHelper
    {
        public static string GenerateToekn(TokenParameter tokenParameter,Users user)
        {

            var id=user.Id.ToString();

            var claims = new[]
            {
                new Claim("nickname", user.Nickname ?? ""),
                new Claim("username", user.Username),
                new Claim("rolesId", user.RolesId.ToString()),
                new Claim("id",id),
                new Claim("txcolor",user.Txcolor),
                new Claim("bgcolor",user.Bgcolor)
            };

            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(tokenParameter.Secret));
            var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var jwtToken = new JwtSecurityToken(tokenParameter.Issuer, null, claims, expires: DateTime.UtcNow.AddMinutes(tokenParameter.AccessExpiration), signingCredentials: credentials);

            var token = new JwtSecurityTokenHandler().WriteToken(jwtToken);

            return token;
        }
    }
}